package com.kh.first.controller;

import com.kh.first.pojo.DepartmentPojo;
import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.pojo.KHDevicePojo;
import com.kh.first.pojo.common.LayTableJson;
import com.kh.first.pojo.common.PageParam;

import com.kh.first.service.DepartmentService;
import com.kh.first.service.KHDeviceService;
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("kHDeviceController")
public class KHDeviceController {
    static final String currentFolder = UserUtils.THEME + "khdevice/";
    @Autowired
    private KHDeviceService kHDeviceService;

    @Autowired
    DepartmentService departmentService;

    @RequestMapping(params = "goKHDeviceList")
    public String goKHDeviceList() {
    return currentFolder + "khdevice-list";
    }


    @RequestMapping(params = "dataGrid")
    @ResponseBody
    public LayTableJson<KHDevicePojo> dataGrid(KHDevicePojo pojo, PageParam pageParam) {
        LayTableJson<KHDevicePojo> ajaxJson = new LayTableJson<>();
        int limit = pageParam.getLimit();
        int page = pageParam.getPage();
        int size = limit == 0 ? 10 : limit;
        pojo.setStart(page == 0 ? 0 : (page - 1) * size);
        pojo.setSize(size);
        List<KHDevicePojo> KHDeviceList = kHDeviceService.getKHDeviceList(pojo);
        ajaxJson.data = KHDeviceList;
        ajaxJson.count = kHDeviceService.countKHDeviceList(pojo).intValue();
        return ajaxJson;
    }

    @RequestMapping(params = "goKHDeviceAdd")
    public String goKHDeviceAdd(Model model) {
        KHDevicePojo pojo=new KHDevicePojo();
        Map<String, Object> map = new HashMap<>();
        map.put("update", 0);
        model.addAttribute("setting", map);
        List<DepartmentPojo> departmentList = departmentService.getDepartmentList(null);
        model.addAttribute("departmentList", departmentList);
        model.addAttribute("khdevice", pojo);
        return currentFolder + "khdevice-add-update";
    }

    @RequestMapping(params = "goKHDeviceQuery")
    public String goKHDeviceQuery(Model model,KHDevicePojo pojo){
        model.addAttribute("khdevice", pojo);
        return currentFolder +"khdevice-query";
    }

    @RequestMapping(params = "goKHDeviceUpdate")
    public String goKHDeviceUpdate(KHDevicePojo khdevice, Model model) {
        List<KHDevicePojo> KHDeviceList = kHDeviceService.getKHDeviceList(khdevice);
        khdevice = KHDeviceList.get(0);//报错让他报
        Map<String, Object> map = new HashMap<>();
        map.put("update", 1);
        model.addAttribute("setting", map);
        model.addAttribute("khdevice",khdevice);
        return currentFolder + "khdevice-add-update";
    }

    @RequestMapping(params = "doKHDeviceAdd")
    @ResponseBody
    public AjaxJson<String> doUserAdd(KHDevicePojo khdevice) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        kHDeviceService.saveKHDevice(khdevice);
        return ajaxJson;
    }
    @RequestMapping(params = "doKHDeviceUpdate")
    @ResponseBody
    public AjaxJson<String> doKHDeviceUpdate(KHDevicePojo khdevice) {
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        kHDeviceService.updateKHDeviceById(khdevice);
        return ajaxJson;
    }

}
